Page({
  data: {
      isincome:true,
      paylist:[],
      incomelist:[],
      incometotal:0,
      paytotal:0,
      dateValue:'',
      datePay:[],
      dateincome:[],
      total:{},
      vv:0
  },
  click(e){
    console.log(123)
    if(e.currentTarget.dataset.name=='income'){
      this.setData({
        isincome:true
      })
    }else{
      console.log(22)
        this.setData({
          isincome:false
        })
    }
  },
  input(e){
    this.setData({
      vv:e.detail.value
    })
  },
  datechange(e){
    let v = e.detail.value
    this.setData({
      dateValue:v
    })
  },
  add(){
    if(this.data.vv.trim()!=''){
      let value = Number(this.data.vv)
      if(this.data.isincome){
          let incomelist = this.data.incomelist
          incomelist.push(value)
          let incometotal = incomelist.reduce((sum,v)=>{
            return sum +=v
          })
          let d = this.data.dateincome
          d.push(this.data.dateValue)
          this.setData({
            incomelist:incomelist,
            incometotal,
            input:'',
            dateincome:d
          })
      }else{
        let paylist = this.data.paylist
        paylist.push(value)
        let paytotal = paylist.reduce((sum,v)=>{
          return sum+=v
        })
        
        let d = this.data.datePay
        d.push(this.data.dateValue)
        this.setData({
          paylist:paylist,
          paytotal,
          input:'',
          datePay:d
        })
      }
      this.add2();
    }
  },
  add2(){
    let dateincome = this.data.dateincome
    let datePay = this.data.datePay
    let total={}
    dateincome.forEach((res,index)=>{
      if(!total.hasOwnProperty(res.substr(0,7))){
            total[res.substr(0,7)]=[]
            total[res.substr(0,7)][0] =0
            total[res.substr(0,7)][1] =0      
            total[res.substr(0,7)][0]=this.data.incomelist[index]+total[res.substr(0,7)][0]
      }else{
        total[res.substr(0,7)][0]=this.data.incomelist[index]+total[res.substr(0,7)][0]
      }
    })
    datePay.forEach((res,index)=>{
      if(!total.hasOwnProperty(res.substr(0,7))){
            total[res.substr(0,7)]=[]
            total[res.substr(0,7)][0] =0
            total[res.substr(0,7)][1] =0      
            total[res.substr(0,7)][1]=this.data.paylist[index]+total[res.substr(0,7)][1]
      }else{
        total[res.substr(0,7)][1]=this.data.paylist[index]+total[res.substr(0,7)][1]
      }
    })
    this.setData({
      total:total
    })
   
  },

  onShow: function () {
      wx.getStorage({
        key: 'money',
      }).then(res=>{
        this.setData({
          incomelist:res.data[0],
          paylist:res.data[1],
          incometotal:res.data[2],
          paytotal:res.data[3],
          dateincome:res.data[4],
          datePay:res.data[5]
        })
        this.add2();
      })
  },
/*  qc(){
    wx.clearStorage({
      success: (res) => {
        console.log('成功')
      },
    })
  }, */
  onHide: function () {
      let store = [this.data.incomelist,this.data.paylist,this.data.incometotal,this.data.paytotal,this.data.dateincome,this.data.datePay]
      wx.setStorage({
        data: store,
        key: 'money',
      })
  },
  onUnload: function () {
      this.onHide();
  }
})